package com.Alan.dao.role;

import com.Alan.dao.BaseDao;
import com.Alan.pojo.Role;
import org.junit.Test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl implements RoleDao{
    @Override
    public List<Role> getRoleList(Connection connection, int userRoleNow) {
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;
        String sql = "select * from role where id > ?;";
        List<Role> roleList = new ArrayList<Role>();
        Object[] params  ={userRoleNow};
        if (connection != null){
            try {
                resultSet = BaseDao.execute(connection, preparedStatement, resultSet, sql, params);
                while (resultSet.next()){
                    Role role = new Role();
                    role.setId(resultSet.getInt("id"));
                    role.setRoleCode(resultSet.getString("roleCode"));
                    role.setRoleName(resultSet.getString("roleName"));
                    roleList.add(role);
                }
            } catch (SQLException throwables) {
                throwables.printStackTrace();
                System.out.println("RoleDaoImpl -> getRoleList");
            }

            BaseDao.closeResource(null, preparedStatement, resultSet);
        }

        return roleList;
    }

    @Test
    public void text(){
        RoleDaoImpl roleDao = new RoleDaoImpl();
        List<Role> roleList = roleDao.getRoleList(BaseDao.getConnection(), 0);
        for (Role role : roleList) {
            System.out.println(role.toString());
        }

    }
}
